<template>
	<view class="cart">
		<!-- 空购物车提示 -->
		<view class="empty" v-if="!cartList.length">
			<image src="/static/images/empty-cart.png" mode="aspectFit"></image>
			<text>购物车还是空的</text>
			<view class="btn" @click="navTo('/pages/goods/list')">去逛逛</view>
		</view>

		<!-- 购物车列表 -->
		<scroll-view class="cart-list" scroll-y v-else>
			<view class="list">
				<view class="cart-item" v-for="(item, index) in cartList" :key="index" 
					:class="{'no-stock': item.stock <= 0}">
					<view class="checkbox" @tap="toggleCheck(index)">
						<u-icon :name="item.stock > 0 ? (item.checked ? 'checkmark-circle-fill' : 'circle') : 'close-circle'"
							:color="item.stock > 0 ? (item.checked ? '#018d71' : '#999') : '#ff5722'"
							:custom-style="item.checked ? checkedStyle : uncheckedStyle" size="40"></u-icon>
					</view>
					<image :src="item.cover" mode="aspectFill"></image>
					<view class="info">
						<view class="name text-ellipsis">{{item.name}}</view>
						<view class="spec">{{item.spec_name}}</view>
						<view class="stock-tip" v-if="item.stock <= 0">
							<u-icon name="info-circle" size="24" color="#ff5722"></u-icon>
							<text>暂无库存</text>
						</view>
						<view class="price-box">
							<text class="price">¥{{item.price}}</text>
							<view class="num-box">
								<text class="minus" :class="{disabled: item.num <= 1 || item.stock <= 0}"
									@tap="updateNum(index, -1)">-</text>
								<text class="num">{{item.num}}</text>
								<text class="plus" :class="{disabled: item.num >= item.stock || item.stock <= 0}"
									@tap="updateNum(index, 1)">+</text>
							</view>
						</view>
					</view>
					<view class="delete" @tap="deleteItem(index)">
						<u-icon name="trash" color="#999" size="40"></u-icon>
					</view>
				</view>
			</view>
		</scroll-view>

		<!-- 底部结算栏 -->
		<view class="footer" v-if="cartList.length">
			<view class="checkbox" @tap="toggleAllCheck">
				<u-icon :name="allChecked ? 'checkbox-marked' : 'checkbox'" :color="allChecked ? '#018d71' : '#999'"
					:custom-style="allChecked ? checkedStyle : uncheckedStyle" size="40"></u-icon>
				<text>全选</text>
			</view>
			<view class="total">
				<text>合计:</text>
				<text class="price">¥{{totalPrice}}</text>
			</view>
			<view class="submit" :class="{disabled: !checkedNum}" @tap="submit">
				结算({{checkedNum}})
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cartList: [],
				checkedStyle: {
					fontWeight: 'bold'
				},
				uncheckedStyle: {
					border: '4rpx solid #999',
					borderRadius: '50%',
					backgroundColor: '#fff'
				}
			}
		},
		computed: {
			// 是否全选
			allChecked() {
				return this.cartList.length > 0 && this.cartList.every(item => item.checked)
			},
			// 选中的商品数量
			checkedNum() {
				return this.cartList.filter(item => item.checked).length
			},
			// 总价
			totalPrice() {
				return this.cartList.reduce((total, item) => {
					if (item.checked) {
						return total + (Number(item.price) * Number(item.num))
					}
					return total
				}, 0).toFixed(2)
			}
		},
		onShow() {
			this.getCartList()
			this.checkUnpaidOrder()
		},
		methods: {
			// 获取购物车列表
			getCartList() {
				vk.callFunction({
					url: 'client/cart/kh/list',
					title: '请求中...',
					data: {
						uid: vk.getVuex('$user.userInfo._id')
					},
					success: (data) => {
						if (data.rows) {
							// 初始化选中状态
							this.cartList = data.rows.map(item => ({
								...item,
								checked: false
							}))
						}
					}
				});
			},
			// 切换选中状态
			toggleCheck(index) {
				if (this.cartList[index].stock <= 0) {
					return;
				}
				this.$set(this.cartList[index], 'checked', !this.cartList[index].checked)
			},
			// 切换全选状态
			toggleAllCheck() {
				const checked = !this.allChecked
				this.cartList.forEach((item, index) => {
					this.$set(this.cartList[index], 'checked', checked)
				})
			},
			// 更新商品数量
			updateNum(index, num) {
				const item = this.cartList[index]
				if (item.stock <= 0) {
					return;
				}
				if (num < 0 && item.num <= 1) return
				if (num > 0 && item.num >= item.stock) return

				const newNum = item.num + num
				this.$set(this.cartList[index], 'num', newNum)

				// 更新购物车数据
				vk.callFunction({
					url: 'client/cart/kh/updateNum',
					data: {
						id: item._id,
						num: newNum
					}
				})
			},
			// 删除商品
			deleteItem(index) {
				uni.showModal({
					title: '提示',
					content: '确定要删除该商品吗？',
					success: res => {
						if (res.confirm) {
							const item = this.cartList[index]
							vk.callFunction({
								url: 'client/cart/kh/remove',
								data: {
									id: item._id
								},
								success: () => {
									this.cartList.splice(index, 1)
									uni.showToast({
										title: '删除成功'
									})
								}
							})
						}
					}
				})
			},
			// 提交订单
			submit() {
				if (!this.checkedNum) {
					uni.showToast({
						title: '请选择商品',
						icon: 'none'
					})
					return
				}

				const checkedGoods = this.cartList.filter(item => item.checked).map(item => ({
					name: item.name,
					cover: item.cover,
					goods_id: item.goods_id,
					spec_name: item.spec_name,
					num: item.num,
					price: item.price,
					cart_id: item._id
				}))

				// 跳转到确认订单页
				uni.navigateTo({
					url: `/pages/order/create?goods=${JSON.stringify(checkedGoods)}`
				})
			},
			// 页面跳转
			navTo(url) {
				uni.switchTab({
					url
				})
			},
			// 检查未支付订单
			checkUnpaidOrder() {
				vk.callFunction({
					url: 'client/order/kh/getUnpaid',
					data: {
						uid: vk.getVuex('$user.userInfo._id'),
						status: 0,
					},
					success: (data) => {
						if (data && data.item) {
							uni.showModal({
								title: '未支付订单提醒',
								content: '您有未支付的订单，是否立即支付？',
								success: (res) => {
									if (res.confirm) {
										uni.navigateTo({
											url: `/pages/order/detail?id=${data.item._id}`
										})
									}
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.cart {
		min-height: 100vh;
		background-color: #f8f8f8;
		padding-bottom: 120rpx;

		.empty {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding-top: 200rpx;

			image {
				width: 300rpx;
				height: 300rpx;
				margin-bottom: 40rpx;
			}

			text {
				font-size: 30rpx;
				color: #999;
				margin-bottom: 50rpx;
			}

			.btn {
				width: 240rpx;
				height: 88rpx;
				line-height: 88rpx;
				text-align: center;
				background-color: #018d71;
				color: #fff;
				border-radius: 44rpx;
				font-size: 30rpx;
				box-shadow: 0 8rpx 16rpx rgba(1, 141, 113, 0.2);

				&:active {
					transform: translateY(2rpx);
				}
			}
		}

		.cart-list {
			padding: 20rpx;

			.cart-item {
				display: flex;
				align-items: center;
				padding: 30rpx;
				background-color: #fff;
				margin-bottom: 20rpx;
				border-radius: 16rpx;
				box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);

				.checkbox {
					width: 80rpx;
					height: 80rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					position: relative;

					::v-deep .u-icon {
						box-sizing: border-box;

						&__icon {
							font-weight: bold;
							font-size: 44rpx !important;
						}
					}

					&::after {
						content: '';
						position: absolute;
						left: 50%;
						top: 50%;
						transform: translate(-50%, -50%);
						width: 80rpx;
						height: 80rpx;
						border-radius: 50%;
						background-color: rgba(1, 141, 113, 0.1);
						opacity: 0;
						transition: opacity 0.2s;
					}

					&:active::after {
						opacity: 1;
					}
				}

				image {
					width: 160rpx;
					height: 160rpx;
					border-radius: 12rpx;
					margin-right: 30rpx;
					box-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.05);
				}

				.info {
					flex: 1;
					overflow: hidden;

					.name {
						font-size: 30rpx;
						color: #333;
						margin-bottom: 16rpx;
						font-weight: 500;
					}

					.spec {
						font-size: 26rpx;
						color: #666;
						margin-bottom: 24rpx;
						background-color: #f8f8f8;
						padding: 6rpx 16rpx;
						border-radius: 6rpx;
						display: inline-block;
					}

					.stock-tip {
						font-size: 24rpx;
						color: #ff5722;
						margin: 8rpx 0;
					}

					.price-box {
						display: flex;
						justify-content: space-between;
						align-items: center;

						.price {
							font-size: 36rpx;
							color: #f00;
							font-weight: bold;
						}

						.num-box {
							display: flex;
							align-items: center;
							background-color: #f8f8f8;
							border-radius: 12rpx;
							padding: 4rpx;

							text {
								width: 68rpx;
								height: 68rpx;
								line-height: 68rpx;
								text-align: center;
								font-size: 28rpx;

								&.minus,
								&.plus {
									background-color: #fff;
									border-radius: 8rpx;
									color: #333;
									font-weight: bold;
									box-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.05);
									position: relative;

									&::after {
										content: '';
										position: absolute;
										left: 0;
										top: 0;
										width: 100%;
										height: 100%;
										background-color: rgba(0, 0, 0, 0.05);
										border-radius: 8rpx;
										opacity: 0;
										transition: opacity 0.2s;
									}

									&:active::after {
										opacity: 1;
									}

									&.disabled {
										color: #ccc;
										background-color: #f5f5f5;
										box-shadow: none;
									}
								}

								&.num {
									font-size: 30rpx;
									color: #333;
									font-weight: 500;
								}
							}
						}
					}
				}

				.delete {
					width: 80rpx;
					height: 80rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					position: relative;

					&::after {
						content: '';
						position: absolute;
						left: 50%;
						top: 50%;
						transform: translate(-50%, -50%);
						width: 80rpx;
						height: 80rpx;
						border-radius: 50%;
						background-color: rgba(0, 0, 0, 0.05);
						opacity: 0;
						transition: opacity 0.2s;
					}

					&:active::after {
						opacity: 1;
					}
				}

				// 无库存商品样式
				&.no-stock {
					opacity: 0.8;
					background-color: #fafafa;
					border: 2rpx solid #f5f5f5;
					
					.info {
						.name {
							color: #999;
						}
						
						.spec {
							background-color: #f0f0f0;
							color: #999;
						}
						
						.stock-tip {
							display: flex;
							align-items: center;
							background-color: #fff5f0;
							padding: 8rpx 16rpx;
							border-radius: 8rpx;
							margin: 16rpx 0;
							
							text {
								margin-left: 8rpx;
								font-size: 24rpx;
								color: #ff5722;
							}
						}
						
						.price-box {
							.price {
								color: #999;
							}
							
							.num-box {
								background-color: #f0f0f0;
								
								.minus, .plus, .num {
									color: #999;
								}
							}
						}
					}
				}
			}
		}

		.footer {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			height: 120rpx;
			background-color: #fff;
			display: flex;
			align-items: center;
			padding: 0 30rpx;
			box-shadow: 0 -2rpx 20rpx rgba(0, 0, 0, 0.08);

			.checkbox {
				display: flex;
				align-items: center;
				padding: 20rpx;
				position: relative;

				&::after {
					content: '';
					position: absolute;
					left: 50%;
					top: 50%;
					transform: translate(-50%, -50%);
					width: 100rpx;
					height: 100rpx;
					border-radius: 50%;
					background-color: rgba(1, 141, 113, 0.1);
					opacity: 0;
					transition: opacity 0.2s;
				}

				&:active::after {
					opacity: 1;
				}

				text {
					font-size: 28rpx;
					color: #333;
					margin-left: 12rpx;
				}
			}

			.total {
				flex: 1;
				text-align: right;
				padding-right: 30rpx;
				font-size: 28rpx;

				.price {
					color: #f00;
					font-size: 40rpx;
					font-weight: bold;
					margin-left: 12rpx;
				}
			}

			.submit {
				width: 240rpx;
				height: 88rpx;
				line-height: 88rpx;
				text-align: center;
				background: linear-gradient(135deg, #02a584 0%, #018d71 100%);
				color: #fff;
				border-radius: 44rpx;
				font-size: 30rpx;
				font-weight: 500;
				box-shadow: 0 8rpx 16rpx rgba(1, 141, 113, 0.2);
				transition: transform 0.2s;

				&:active {
					transform: translateY(2rpx);
				}

				&.disabled {
					background: #ccc;
					box-shadow: none;
				}
			}
		}
	}

	.text-ellipsis {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
</style>